import { ConfigType, registerAs } from '@nestjs/config'
import { env, envBoolean, envNumber } from '../common/global/env'

export const appRegToken = 'app'

export const AppConfig = registerAs(appRegToken, () => ({
  name: env('APP_NAME'),
  port: envNumber('APP_PORT', 3000),
  baseUrl: env('APP_BASE_URL'),
  globalPrefix: env('GLOBAL_PREFIX', 'api'),
  locale: env('APP_LOCALE', 'zh-CN'),
  /** 是否允许多端登录 */
  multiDeviceLogin: envBoolean('MULTI_DEVICE_LOGIN', true),

  logger: {
    level: env('LOGGER_LEVEL'),
    maxFiles: envNumber('LOGGER_MAX_FILES'),
  },
}))

export type IAppConfig = ConfigType<typeof AppConfig>

export default (): { app: IAppConfig } => ({
  app: {
    name: 'NEST-DRIZZLE API',
    port: parseInt(process.env.PORT || '3000', 10),
    baseUrl: process.env.APP_BASE_URL || 'http://localhost:3000',
    globalPrefix: process.env.GLOBAL_PREFIX || 'api',
    locale: process.env.APP_LOCALE || 'zh-CN',
    multiDeviceLogin: process.env.MULTI_DEVICE_LOGIN === 'true',
    logger: {
      level: process.env.LOGGER_LEVEL || 'info',
      maxFiles: parseInt(process.env.LOGGER_MAX_FILES || '7', 10),
    }
  },
});
